package com.jiangjiang.eshop.membership.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.membership.constant.MemberLevel;
import com.jiangjiang.eshop.membership.dao.MemberLevelMapper;
import com.jiangjiang.eshop.membership.domain.MemberLevelEntity;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Objects;

/**
 * @ClassName MemberLevelService
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/4 17:49
 * @Version 1.0
 **/
@Service
public class MemberLevelService extends ServiceImpl<MemberLevelMapper, MemberLevelEntity> {

    /**
     * 根据用户账户查询会员等级
     * @param accountId 用户账户id
     * @return 会员等级记录
     */
    public MemberLevelEntity queryByAccountId(Long accountId){
        LambdaQueryWrapper<MemberLevelEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberLevelEntity::getUserAccountId, accountId);
        MemberLevelEntity entity = this.getOne(wrapper);
        if(Objects.isNull(entity)){
            // 创建一个新的memberLevel
            entity = new MemberLevelEntity();
            entity.setUserAccountId(accountId);
            entity.setLevel(MemberLevel.BRONZE);
            entity.setGrowthValue(0L);
            this.save(entity);
        }
        return entity;
    }

}
